# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openimageio

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.4.8
pkgrel=2
pkgdesc="A library for reading and writing images, including classes, utilities, and applications (mingw-w64)"
arch=('any')
url="http://www.openimageio.org/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
        "${MINGW_PACKAGE_PREFIX}-field3d"
        "${MINGW_PACKAGE_PREFIX}-freetype"
        "${MINGW_PACKAGE_PREFIX}-jasper"
        "${MINGW_PACKAGE_PREFIX}-giflib"
        "${MINGW_PACKAGE_PREFIX}-glew"
        "${MINGW_PACKAGE_PREFIX}-hdf5"
        "${MINGW_PACKAGE_PREFIX}-libjpeg"
        "${MINGW_PACKAGE_PREFIX}-libpng"
        "${MINGW_PACKAGE_PREFIX}-LibRaw"
        "${MINGW_PACKAGE_PREFIX}-libwebp"
        "${MINGW_PACKAGE_PREFIX}-libtiff"
        "${MINGW_PACKAGE_PREFIX}-opencolorio"
        "${MINGW_PACKAGE_PREFIX}-opencv"
        "${MINGW_PACKAGE_PREFIX}-openexr"
        "${MINGW_PACKAGE_PREFIX}-openjpeg"
        "${MINGW_PACKAGE_PREFIX}-openssl"
        "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-qt5" "${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-boost")
optdepends=(#"${MINGW_PACKAGE_PREFIX}-qt4: iv image viewer"
            "${MINGW_PACKAGE_PREFIX}-python2: bindings support")
options=('strip' 'buildflags')
source=(https://github.com/OpenImageIO/oiio/archive/Release-$pkgver.tar.gz
        fix-mingw-w64.patch
        workaround-ansidecl-h-PTR-define-conflict.patch
        giflib.patch)
md5sums=('fff0d9673fcd9b8cb050c91af5cfd731'
         '8da0d1c2140b8ffa707a6dbe3874a1ab'
         '1bf4c840ee73d37ebdfc98d3ea527ed4'
         'f5cef862b0700c80cb574bfec9625b85')

prepare() {
  cd ${srcdir}/oiio-Release-${pkgver}
  patch -p1 -i ${srcdir}/fix-mingw-w64.patch
  patch -p1 -i ${srcdir}/workaround-ansidecl-h-PTR-define-conflict.patch
  patch -p1 -i ${srcdir}/giflib.patch
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null
  
  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null
  
  export OPENJPEG_HOME=${MINGW_PREFIX}
  [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}
  
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${pkgdir}${MINGW_PREFIX} \
    -DPYLIB_INSTALL_DIR=lib/python2.7/site-packages \
    -DUSE_FREETYPE=ON \
    -DFREETYPE_INCLUDE_PATH=${PREFIX_DEPS}/include/freetype2 \
    -DOPENJPEG_INCLUDE_DIR=${PREFIX_DEPS}/include/openjpeg-1.5 \
    -DOPENJPEG_OPENJPEG_LIBRARIES=${PREFIX_DEPS}/lib/libopenjpeg.dll.a \
    -DUSE_OPENCV=ON \
    -DUSE_FIELD3D=ON \
    -DCMAKE_SHARED_LINKER_FLAGS=" -Wl,--export-all-symbols -Wl,--enable-auto-import " \
    -DOIIO_BUILD_TESTS=ON \
    ../oiio-Release-${pkgver}
    
    #-DVERBOSE=ON
    #-DCMAKE_VERBOSE_MAKEFILE=ON
  make
}

package() {
  cd "$srcdir"/build-${MINGW_CHOST}

  make install
  rm -r ${pkgdir}${MINGW_PREFIX}/doc

  install -Dm644 ${srcdir}/oiio-Release-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname/LICENSE"
}
